package com.example.demo.service.impl;

import com.example.demo.entity.Personnelmanagepeople;
import com.example.demo.handle.PeopleListParseHandler;
import com.example.demo.mapper.PersonnelmanagepeopleMapper;
import com.example.demo.service.PersonnelmanagepeopleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-21
 */
@Service
public class PersonnelmanagepeopleServiceImpl extends ServiceImpl<PersonnelmanagepeopleMapper, Personnelmanagepeople> implements PersonnelmanagepeopleService {

    @Autowired
    private PersonnelmanagepeopleMapper personnelmanagepeopleMapper;

    @Override
    public Integer insertPeopleData(String peopleXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            PeopleListParseHandler peopleListParseHandler = new PeopleListParseHandler();
            try {
                //读取井口信息文件
                parser.parse(peopleXml, peopleListParseHandler);

            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + peopleListParseHandler.getPersonnelmanagepeopleArrayList().size() + "个信息");
            // 根据minecode去查找数据库
            for (Personnelmanagepeople personnelmanagepeople : peopleListParseHandler.getPersonnelmanagepeopleArrayList()) {
                System.out.println(personnelmanagepeople.getCsMineCode());
                System.out.println(personnelmanagepeople.getCsDataTime());
                System.out.println(personnelmanagepeople.getPsPersonCard());
                System.out.println(personnelmanagepeople.getPsPersonName());
                System.out.println(personnelmanagepeople.getPsIdNumber());
                System.out.println(personnelmanagepeople.getPsPersonSex());
                System.out.println(personnelmanagepeople.getPsPersonBirthday());
                System.out.println(personnelmanagepeople.getPsPersonBlood());
                System.out.println(personnelmanagepeople.getPsPersonEdu());
                System.out.println(personnelmanagepeople.getPsPersonMarry());
                System.out.println(personnelmanagepeople.getPsPersonPhone());
                System.out.println(personnelmanagepeople.getPsPersonEmer());
                System.out.println(personnelmanagepeople.getPsPersonAddr());
                System.out.println(personnelmanagepeople.getPsPersonPost());
                System.out.println(personnelmanagepeople.getPsPersonDuty());
                System.out.println(personnelmanagepeople.getPsPersonDept());
                System.out.println(personnelmanagepeople.getPsWorkPlace());
                System.out.println(personnelmanagepeople.getPsPersonDutyname());
                System.out.println(personnelmanagepeople.getPsPersonDutynum());
                System.out.println(personnelmanagepeople.getPsPersonDutydate());


                System.out.println("----------------------------安全监控信息finish---------------------------------");

                personnelmanagepeopleMapper.insertPeopleData(personnelmanagepeople.getCsMineCode(),personnelmanagepeople.getCsDataTime(),personnelmanagepeople.getPsPersonCard(),personnelmanagepeople.getPsPersonName(),personnelmanagepeople.getPsIdNumber(),personnelmanagepeople.getPsPersonSex(),personnelmanagepeople.getPsPersonBirthday(),personnelmanagepeople.getPsPersonBlood(),personnelmanagepeople.getPsPersonEdu(),personnelmanagepeople.getPsPersonMarry(),personnelmanagepeople.getPsPersonPhone(),personnelmanagepeople.getPsPersonEmer(),personnelmanagepeople.getPsPersonAddr(),personnelmanagepeople.getPsPersonPost(),personnelmanagepeople.getPsPersonDuty(),personnelmanagepeople.getPsPersonDept(),personnelmanagepeople.getPsWorkPlace(),personnelmanagepeople.getPsPersonDutyname(),personnelmanagepeople.getPsPersonDutynum(),personnelmanagepeople.getPsPersonDutydate());
            }


        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
